package threadpool;

import thread.ThreadUtils;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecuteDemo {

    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 15, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>(40));
        for (int i = 1; i <= 50; i++) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("当前线程：" + Thread.currentThread().getName());
                }
            });
            ThreadUtils.sleep(150);
            System.out.println("第" + i + "个任务");
            System.out.println("活跃线程数：" + threadPool.getActiveCount());
            System.out.println("已完成任务数：" + threadPool.getCompletedTaskCount());
            System.out.println("核心线程数：" + threadPool.getCorePoolSize());
            System.out.println("当前线程数：" + threadPool.getPoolSize());
            System.out.println("线程池中曾同时存在的最大线程数：" + threadPool.getLargestPoolSize());
            System.out.println("等待被执行的任务队列：" + threadPool.getQueue());
            System.out.println("等待被执行的任务队列大小：" + threadPool.getQueue().size());
            System.out.println("线程池的最大容量：" + threadPool.getMaximumPoolSize());
            System.out.println("尚未执行的任务数量：" + threadPool.getTaskCount());
            System.out.println("拒绝执行处理器：" + threadPool.getRejectedExecutionHandler());
            System.out.println("线程工厂：" + threadPool.getThreadFactory());
            System.out.println();
        }
        threadPool.shutdown();
    }
}
